//
//  YZMComposeTextVew.swift
//  YZMweibo
//
//  Created by Simon on 16/4/24.
//  Copyright © 2016年 Simon. All rights reserved.
//

import UIKit

class YZMComposeTextVew: UITextView {
    //重写父类方法
    override var font: UIFont?{
        didSet{
            textLabelCompose.font = font
        
        }
    }
     // 供外界设置占位文字
    var placeholder: String?{
        didSet{
        textLabelCompose.text = placeholder
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置视图
    private func setUI(){
    
        self.backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(textLabelCompose)
        //约束
        textLabelCompose.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(5)
            make.top.equalTo(8)
            make.width.equalTo(ScreenW - 10)
        }
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textLabelComposeChange", name: UITextViewTextDidChangeNotification, object: nil)
    }
    //监听文字改变 隐藏站位
    @objc private func textLabelComposeChange(){
        textLabelCompose.hidden = self.hasText()
        
    }

    //懒加载控件
    private lazy var textLabelCompose: UILabel = {
    
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(12)
        label.textColor = UIColor.whiteColor()
        label.numberOfLines = 0
        return label
    }()
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}
